/* Copyright (C)
 * 2017 - Xianlee.Wu  xianleewu@163.com
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 */

#ifndef __LEWEI_CTRL_H__
#define __LEWEI_CTRL_H__

#include "pthread.h"

#define MAX_DATA_SIZE           (1024*4)
#define RECONNECT_TIME          (20)
#define CONNECTION_TIMEOUT      (6000L)
#define LEWEI_SERVER_ADDR       "tcp.lewei50.com"
#define LEWEI_SERVER_PORT       9960
#define LEWEI_USERKEY_MAX_SIZE  (128)

#define LEWEI_CMD_F_NODE        "f1"
#define LEWEI_CMD_P1_NODE       "p1"

#define LEWEI_CMD_TOGGLE        ":\"toggle\""
#define LEWEI_CMD_TOGGLE_TRUE   "\"p1\":\"on\""
#define LEWEI_CMD_TOGGLE_FALSE  "\"p1\":\"off\""
#define LEWEI_REG_FORMAT        "{\"method\":\"update\",\"gatewayNo\":\"%02d\",\"userkey\": \"%s\"}&^!"

typedef struct _lewei_client {
    char userkey[LEWEI_USERKEY_MAX_SIZE];
    int gateway_num;
    int socket_fd;
    int is_connected;
    int is_registered;
    int is_running;
    pthread_mutex_t mutex;
} lewei_client_t;


int lewei_ctrl_init(lewei_client_t* client, int gateway_num, const char* userkey);

int lewei_ctrl_destroy(lewei_client_t* client);

void* lewei_ctrl_recv_cmd(void* arg);

void* lewei_ctrl_check_connection(void* arg);

void* lewei_ctrl_keep_connection(void *arg);

#endif
